//index.js
var app = getApp()
Page({
  data: {
    navindex: 0,
    nav: [
      { txt: '全部订单' },
      { txt: '待付款' },
      { txt: '代发货' },
      { txt: '待收货' },
      { txt: '已完成' },
    ],
    orderdeil:[
    //   {
    //     urls: "../../images/banner1.jpg",
    //     name: "需使用4G飞享套餐。赠送12GB流量，2400分钟通话",
    //     price: "1370",
    //     shuliang: "1",
    //     date: '2016-03-06 11:26',
    //     state:'待发货',
    //     number:'333658987',
    //   },
    //  {
    //     urls: "../../images/banner1.jpg",
    //     name: "需使用4G飞享套餐。赠送12GB流量，2400分钟通话",
    //     price: "1370",
    //     shuliang: "1",
    //     date: '2016-03-06 11:26',
    //     number: '333658987',
    //   },
      
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    userInfo: {}
  },
  click: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    
    this.setData({
      navindex: id
    })
    wx.request({
      url: app.appUrl + '/Order/MyOrderList',
      data: {
        bid: wx.getStorageSync('bid'),
        uid: wx.getStorageSync('uid'),
        status: id
      },
      success: function (e) {
        console.log('商品列表', e);
        that.setData({
          orderdeil: e.data.data
        })
      }
    })
  },
  toOrder:function(){
    app.navTo('../order/order')
  },
  modalcnt: function (e) {
    var id = e.currentTarget.dataset.index;
    var orderid = e.currentTarget.dataset.id;
    var del = this.data.orderdeil;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除当前订单吗？',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          for (var i in del) {
            if (i == id) {
              del.splice(id, 1);
            }
          }
          that.setData({
            orderdeil: del
          })
          wx.request({
            url: app.appUrl + '/Order/deleOrder',
            data: {
              orderid: orderid
            },
            success: function (e) {
              console.log('商品列表', e);
              if(e.data.data==1){
                wx.showToast({
                  title: '删除成功',
                  icon: 'succes',
                  duration: 1000,
                  mask: true
                })
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')

        }
      }
    })
  },
  deleteList(e) {    
    const index = e.currentTarget.dataset.index;
    let orderdeil = this.data.orderdeil;
    console.log(index)
    orderdeil.splice(index, 1);              // 删除购物车列表里这个商品
    this.setData({
      orderdeil: orderdeil
    });
    if (!carts.length) {                  // 如果购物车为空
      this.setData({
        hasList: false              // 修改标识为false，显示购物车为空页面
      });
    } else {                              // 如果不为空
      this.getTotalPrice();           // 重新计算总价格
    }
  },
  cancellation:function(e){
    var id = e.currentTarget.dataset.index;
    var orderid = e.currentTarget.dataset.id;
    var del = this.data.orderdeil;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要取消当前订单吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.appUrl + '/Order/cancellation',
            data: {
              orderid: orderid
            },
            success: function (e) {
              console.log('退款', e);
              if (e.data.data == 1) {
                for (var i in del) {
                  if (i == id) {
                    del.splice(id, 1);
                  }
                }
                that.setData({
                  orderdeil: del
                })
                wx.showToast({
                  title: '退款成功',
                  icon: 'succes',
                  duration: 1000,
                  mask: true
                })
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')

        }
      }
    })
  },
  complete:function(e){
    var id = e.currentTarget.dataset.index;
    var orderid = e.currentTarget.dataset.id;
    var del = this.data.orderdeil;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定当前订单已收货吗？',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          for (var i in del) {
            if (i == id) {
              del.splice(id, 1);
            }
          }
          that.setData({
            orderdeil: del
          })
          wx.request({
            url: app.appUrl + '/Order/complete',
            data: {
              orderid: orderid
            },
            success: function (e) {
              console.log('商品列表', e);
              if (e.data.data == 1) {
                wx.showToast({
                  title: '确认成功',   
                  icon: 'succes',
                  duration: 1000,
                  mask: true
                })
              }
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')

        }
      }
    })
  },
  onShow: function () {
    var that = this;
    wx.request({
      url: app.appUrl + '/Order/MyOrderList',
      data: {
        bid:wx.getStorageSync('bid'),
        uid:wx.getStorageSync('uid'),
        status: 0
      },
      success: function (e) {
        console.log('商品列表', e);
        that.setData({
          orderdeil:e.data.data
        })
      }
    })
  },
})  
